package com.linkage.seapeak.view;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.PopupWindow;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.DateCalcActivity;
import com.linkage.jz.bankruptcy.activity.Main.PenaltyCalcActivity;
import com.linkage.jz.bankruptcy.activity.Main.RateCalcActivity;
import com.linkage.seapeak.utils.CommonUtils;

/**
 * 快速应用弹框
 */
public class QuickPopView {

    public static PopupWindow show(final Activity context) {
        View contentView;
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        contentView = mLayoutInflater.inflate(R.layout.view_pop_quick_tool,
                null);
        final PopupWindow pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        final View dataView = contentView.findViewById(R.id.data_view);
        final View moneyView = contentView.findViewById(R.id.money_view);
        final View interestView = contentView.findViewById(R.id.interest_view);
        final View pop_click = contentView.findViewById(R.id.pop_click);

        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        // 产生背景变暗效果
        WindowManager.LayoutParams lp = context.getWindow()
                .getAttributes();
        lp.alpha = 0.4f;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
        pop.setTouchable(true);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOutsideTouchable(true);
        pop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
        pop.update();
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {

            // 在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow()
                        .getAttributes();
                lp.alpha = 1f;
                context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                context.getWindow().setAttributes(lp);
            }
        });

        contentView.post(new Runnable() {
            @Override
            public void run() {
                ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
                animator.setDuration(400);
                animator.setInterpolator(new OvershootInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        dataView.setAlpha((float) animation.getAnimatedValue());
                        moneyView.setAlpha((float) animation.getAnimatedValue());
                        interestView.setAlpha((float) animation.getAnimatedValue());
                        dataView.setTranslationX(-value);
                        moneyView.setTranslationX(-value * 0.8f);
                        moneyView.setTranslationY(-value * 0.8f);
                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                    }
                });
                animator.start();
            }
        });

        dataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new DecelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        dataView.setAlpha((float) animation.getAnimatedValue());
                        moneyView.setAlpha((float) animation.getAnimatedValue());
                        interestView.setAlpha((float) animation.getAnimatedValue());
                        dataView.setTranslationX(-value);
                        moneyView.setTranslationX(-value * 0.8f);
                        moneyView.setTranslationY(-value * 0.8f);
                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f) {
                            DateCalcActivity.start(context);
                            pop.dismiss();
                        }
                    }
                });
                animator.start();
            }
        });

        moneyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new DecelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        dataView.setAlpha((float) animation.getAnimatedValue());
                        moneyView.setAlpha((float) animation.getAnimatedValue());
                        interestView.setAlpha((float) animation.getAnimatedValue());
                        dataView.setTranslationX(-value);
                        moneyView.setTranslationX(-value * 0.8f);
                        moneyView.setTranslationY(-value * 0.8f);
                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f) {
                            PenaltyCalcActivity.start(context);
                            pop.dismiss();
                        }
                    }
                });
                animator.start();
            }
        });

        interestView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new DecelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        dataView.setAlpha((float) animation.getAnimatedValue());
                        moneyView.setAlpha((float) animation.getAnimatedValue());
                        interestView.setAlpha((float) animation.getAnimatedValue());
                        dataView.setTranslationX(-value);
                        moneyView.setTranslationX(-value * 0.8f);
                        moneyView.setTranslationY(-value * 0.8f);
                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f) {
                            RateCalcActivity.start(context);
                            pop.dismiss();
                        }
                    }
                });
                animator.start();
            }
        });

        pop_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        dataView.setAlpha((float) animation.getAnimatedValue());
                        moneyView.setAlpha((float) animation.getAnimatedValue());
                        interestView.setAlpha((float) animation.getAnimatedValue());
                        dataView.setTranslationX(-value);
                        moneyView.setTranslationX(-value * 0.8f);
                        moneyView.setTranslationY(-value * 0.8f);
                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f)
                            pop.dismiss();
                    }
                });
                animator.start();
            }
        });
        return pop;
    }

}
